//
//  XLRetweetStatusView.swift
//  SinaWeibo
//
//  Created by 李凯 on 16/9/6.
//  Copyright © 2016年 kevin. All rights reserved.
//

import UIKit

class XLRetweetStatusView: UIView {
    
    var bottomCons: Constraint?

    var statusViewModel: XLStatusViewModel? {
        didSet{
            contentLabel.text = statusViewModel?.retweetStautsText
            
            // 先卸载掉之前的约束
            self.bottomCons?.uninstall()
            // 判断转发微博是否有配图数据
            if let pic_urls = statusViewModel?.status?.retweeted_status?.pic_urls , pic_urls.count > 0{
                pictureView.isHidden = false
                pictureView.pic_urls = pic_urls
                self.snp_updateConstraints { (make) -> Void in
                    self.bottomCons = make.bottom.equalTo(pictureView).offset(HMStatusCellMargin).constraint
                }
            }else{
                // 没有配图
                pictureView.isHidden = true;
                self.snp_updateConstraints { (make) -> Void in
                    self.bottomCons = make.bottom.equalTo(contentLabel).offset(HMStatusCellMargin).constraint
                }
            }

        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI() {
        
        backgroundColor = UIColor.gray

        
        addSubview(contentLabel)
        addSubview(pictureView)
        
        contentLabel.numberOfLines = 0
        contentLabel.preferredMaxLayoutWidth = HMScreenW - 2 * HMStatusCellMargin
        
        // 添加约束
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.left.top.equalTo(self).offset(HMStatusCellMargin)
            //make.bottom.equalTo(self).offset(-HMStatusCellMargin)
        }
        
        pictureView.snp_makeConstraints { (make) -> Void in
            make.size.equalTo(CGSize(width: 100, height: 100))
            make.top.equalTo(contentLabel.snp_bottom).offset(HMStatusCellMargin)
            make.left.equalTo(contentLabel)
        }
        
        self.snp_makeConstraints { (make) -> Void in
            self.bottomCons = make.bottom.equalTo(pictureView).offset(HMStatusCellMargin).constraint
        }

    }
    
    lazy var contentLabel: UILabel = {
        let label = UILabel(textColor: .darkGray, fontSize: 13)
        return label
    }()
    
    /// 配图视图
    private lazy var pictureView: XLStatusPictureView = {
        let view = XLStatusPictureView(frame: CGRect.zero, collectionViewLayout: UICollectionViewFlowLayout())
        view.backgroundColor = self.backgroundColor
        return view
    }()



}
